﻿Imports System.Data
Imports System.Data.Odbc

Module dataBlock
    Private dtBlock As New DataTable



    Public Sub listaBlock()

        conectar()
        dtBlock.Clear()

        Cursor.Current = Cursors.WaitCursor
        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM tblock"
        cmd.CommandType = CommandType.Text
        da.SelectCommand = cmd
        da.Fill(dtBlock)
        'Çargamos el Datagridview
        With frmBlock.DGV_Block

            .DefaultCellStyle.Font = New Font("Verdana", 10)
            .AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells
            .AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells
            .ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
            .AlternatingRowsDefaultCellStyle.BackColor = Color.AntiqueWhite

            .DataSource = dtBlock

     
            .Columns(0).ReadOnly = True
            .Columns(0).HeaderText = "Código"
            .Columns(1).HeaderText = "Nombre"
            .Refresh()
        End With

        'Coloreamos intercaladamente el datagridview



        Cursor.Current = Cursors.Default
        desconectar()



    End Sub


    Sub ingresoBlock(ByVal nblock As String)
        conectar()
        cmd = New Odbc.OdbcCommand("INSERT INTO tblock (Nombre) Values('" + nblock + "')", con)
        If cmd.ExecuteNonQuery Then
            listaBlock()
        Else
            MessageBox.Show(msjNoIngresar, msjTituloModal, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

        desconectar()

    End Sub


    Sub actualizarBlock(ByVal id As Integer, ByVal nom As String)

        conectar()

        cmd = New Odbc.OdbcCommand("UPDATE tblock set Nombre='" + nom + "' WHERE ID = '" & id & "'", con)

        If cmd.ExecuteNonQuery Then
            listaBlock()
        Else
            MessageBox.Show(msjNoActualizar, msjTituloModal, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        desconectar()


    End Sub


    Sub eliminarblock(ByVal id As Integer)

        conectar()

        cmd = New Odbc.OdbcCommand("DELETE FROM tblock WHERE ID = '" & id & "'", con)

        If cmd.ExecuteNonQuery Then
            listaBlock()
        Else
            MessageBox.Show(msjNoEliminar, msjTituloModal, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        desconectar()


    End Sub
End Module
